using System;
using System.Collections;
using JetBrains.Annotations;
using UnityEngine;
using Voodoo.Sauce.Common.Utils;
using Voodoo.Sauce.CrashReport;
using Voodoo.Sauce.Debugger;
using Voodoo.Sauce.Internal;
using Voodoo.Sauce.Internal.Misc;
using Voodoo.Sauce.Internal.Utils;
using Voodoo.Sauce.Internal.VoodooTune;
using Voodoo.Sauce.Tools.AccessButton;

namespace Voodoo.Sauce.Core
{
	internal class VoodooSauceBehaviour : MonoBehaviour
	{
		private const string TAG = "VoodooSauceBehaviour";

		private static VoodooSauceBehaviour _instance;

		[SerializeField]
		private CohortDebugMenu _cohortDebugMenuPrefab;

		[SerializeField]
		private GameObject fakeAdsPrefab;

		[SerializeField]
		private GameObject _accessButton;

		private CohortDebugMenu _cohortDebugMenu;

		private VoodooSettings _settings;

		private static bool _startCalled;

		private static bool _initStarted;

		private static bool _initFinished;

		private static string _mediationName;

		public static Action OnInternetConnect;

		public static Action OnPrivacyOpened;

		public static Action OnPrivacyClosed;

		public static Action onDeleteDataRequested;

		private static HandledAction<VoodooSauceInitCallbackResult> _initFinishedEvent = new HandledAction<VoodooSauceInitCallbackResult>("VoodooSauceBehaviour");

		[CanBeNull]
		private static VoodooSauceInitCallbackResult _initCallbackResult;

		private static event Action<bool> OnAnalyticsConsentEvent;

		private async void Awake()
		{
			if (base.transform != base.transform.root)
			{
				throw new Exception("VoodooSauce prefab HAS to be at the ROOT level!");
			}
			_settings = VoodooSettings.Load();
			if (_settings == null)
			{
				throw new Exception("Can't find VoodooSauce settings file. Please check you have created the file using Assets/Create/VoodooSauce/Settings File");
			}
			if (_instance != null)
			{
				UnityEngine.Object.Destroy(base.gameObject);
				return;
			}
			_instance = this;
			UnityEngine.Object.DontDestroyOnLoad(this);
			UnityThreadExecutor.Initialize();
			VoodooSuperPremium.Initialize(_settings);
			VoodooSauceCore.Initialize(this);
			InitVoodooSauce();
		}

		private static void EnableUnityAnalytics()
		{
		}

		[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
		private static void DisableUnityAnalytics()
		{
		}

		private void InitVoodooTune()
		{
			VoodooTuneManager.Initialize(InitVoodooSauce);
		}

		private void InitVoodooSauce()
		{
			if (!_initStarted)
			{
				_initStarted = true;
				InitCrashReport();
				InitAbTest();
			}
		}

		private void InitAbTest()
		{
		}

		private void Start()
		{
			_startCalled = true;
			AccessProcess.InstantiateAccessButton = (AccessProcess.AccessButtonEvent)Delegate.Combine(AccessProcess.InstantiateAccessButton, new AccessProcess.AccessButtonEvent(InstantiateAccessButton));
			AccessProcess.DisposeAccessButton = (AccessProcess.AccessButtonEvent)Delegate.Combine(AccessProcess.DisposeAccessButton, new AccessProcess.AccessButtonEvent(DisposeAccessButton));
			AccessProcess.CheckInstantiateButton();
		}

		public static void SubscribeOnInitFinishedEvent(Action<VoodooSauceInitCallbackResult> onInitFinished)
		{
			if (_initFinished)
			{
				onInitFinished?.Invoke(_initCallbackResult);
			}
			else
			{
				_initFinishedEvent?.Add(onInitFinished);
			}
		}

		public static void UnSubscribeOnInitFinishedEvent(Action<VoodooSauceInitCallbackResult> onInitFinished)
		{
			_initFinishedEvent?.Remove(onInitFinished);
		}

		internal static bool IsInitFinished()
		{
			return _initFinished;
		}

		public static T AddComponent<T>(bool inNewGameObject = false) where T : MonoBehaviour
		{
			if (_instance == null)
			{
				return null;
			}
			GameObject gameObject;
			if (inNewGameObject)
			{
				GameObject obj = new GameObject("T");
				obj.transform.SetParent(_instance.transform);
				gameObject = obj;
			}
			else
			{
				gameObject = _instance.gameObject;
			}
			return gameObject.AddComponent<T>();
		}

		private void InitCrashReport()
		{
			CrashReportCore.CrashReportManagerParameters parameters = new CrashReportCore.CrashReportManagerParameters
			{
				VoodooSettings = _settings,
				AnalyticsConsentEvent = null
			};
			OnAnalyticsConsentEvent += delegate(bool analyticsConsent)
			{
				parameters.AnalyticsConsentEvent?.Invoke(analyticsConsent);
			};
			VoodooSauceCore.GetCrashReport().Initialize(parameters);
		}

		public static void ShowCohortDebugMenu()
		{
			if (!(_instance == null))
			{
				if (_instance._cohortDebugMenu == null)
				{
					_instance._cohortDebugMenu = UnityEngine.Object.Instantiate(_instance._cohortDebugMenuPrefab);
				}
				else
				{
					_instance._cohortDebugMenu.Show();
				}
			}
		}

		public static void CloseCohortDebugMenu()
		{
			if (!(_instance == null) && !(_instance._cohortDebugMenu == null))
			{
				_instance._cohortDebugMenu.Hide();
			}
		}

		public static void OpenDebugger()
		{
			Voodoo.Sauce.Debugger.Debugger.TryOpen();
		}

		public static void CloseDebugger()
		{
			Voodoo.Sauce.Debugger.Debugger.TryClose();
		}

		private void OnApplicationFocus(bool hasFocus)
		{
		}

		private void OnApplicationPause(bool pauseStatus)
		{
			if (!pauseStatus)
			{
				_ = _startCalled;
			}
		}

		internal static void InvokeAfter(Action methodToCall, float duration)
		{
			_instance.StartCoroutine(InvokeAfterCoroutine(methodToCall, duration));
		}

		internal static Coroutine InvokeCoroutine(IEnumerator coroutine)
		{
			if (_instance == null)
			{
				return null;
			}
			return _instance.StartCoroutine(coroutine);
		}

		internal static void KillCoroutine(Coroutine coroutine)
		{
			if (!(_instance == null))
			{
				_instance.StopCoroutine(coroutine);
			}
		}

		private static IEnumerator InvokeAfterCoroutine(Action methodToCall, float duration)
		{
			yield return new WaitForSeconds(duration);
			methodToCall();
		}

		private void InstantiateAccessButton()
		{
			if (AccessProcess.ButtonInstance == null)
			{
				AccessProcess.ButtonInstance = UnityEngine.Object.Instantiate(_accessButton);
			}
		}

		private static void DisposeAccessButton()
		{
			if (AccessProcess.ButtonInstance != null)
			{
				UnityEngine.Object.Destroy(AccessProcess.ButtonInstance);
			}
		}

		private void OnDestroy()
		{
			AccessProcess.InstantiateAccessButton = (AccessProcess.AccessButtonEvent)Delegate.Remove(AccessProcess.InstantiateAccessButton, new AccessProcess.AccessButtonEvent(InstantiateAccessButton));
			AccessProcess.DisposeAccessButton = (AccessProcess.AccessButtonEvent)Delegate.Remove(AccessProcess.DisposeAccessButton, new AccessProcess.AccessButtonEvent(DisposeAccessButton));
		}

		internal static void CallAfterInitialization(Action<VoodooSauceInitCallbackResult> action)
		{
			if (IsInitFinished())
			{
				action(_initCallbackResult);
			}
			else
			{
				SubscribeOnInitFinishedEvent(action);
			}
		}
	}
}
